<template>
	<view>
		<anil-seat v-if="isshow" :title="title" :info="info" :room-name="roomname" :seatData="seatlist"
			@confirm="confirm">
		</anil-seat>
		<u-modal title="提示" v-model="show" content="选座请在同一排且不可留空"></u-modal>
		<aui-loading ref="auiLoading" :type="auiLoading.type" :direction="auiLoading.row" :msg="auiLoading.msg"
			:mask="auiLoading.mask"></aui-loading>
		<u-toast ref="uToast" />
		<u-modal v-model="fkshow" content="您还有一笔未付款订单,是否立即付款" @cancel="cancel()" @confirm="goorderinfo"
			confirm-text="立即付款" cancel-text="取消订单" :show-confirm-button="true" :show-cancel-button="true"
			cancel-color="#55aaff" confirm-color="#55aaff"> </u-modal>
	</view>
</template>

<script>
	import Config from '../../common/config.js';
	import anilSeat from '@/uni_modules/anil-seat/components/anil-seat/anil-seat.vue'
	import {
		aui
	} from '@/components/aui-loading/common/aui/js/aui.js';
	import auiLoading from '@/components/aui-loading/aui-loading.vue';
	// import {
	// 	seatData
	// } from '@/static/seat-data.js'
	export default {
		components: {
			anilSeat,
			auiLoading
		},
		data() {
			return {
				filmId: "",
				item: {},
				fkshow: false,
				auiLoading: {
					show: false,
					type: 3,
					direction: 'row',
					msg: '加载中',
					mask: true,
				},
				getphoneshow: false,
				show: false,
				isshow: false,
				title: "",
				roomname: "影院银幕",
				info: "",
				seatlist: [],
				cinemaId: 0,
				hallId: 0,
				showIndex: 0,
				Price: 0,
				smallPhoto: '',
				hallName: "",
				cinemaName: "",
				showTime: "",
				orderlist: [],
				infos: [],
				duration:"",
				showt:false
			}
		},
		onLoad(pamar) {
			//this.seatlist = uni.getStorageSync("seatlist")
			this.smallPhoto = pamar.smallPhoto
			this.title = pamar.filmName
			this.roomname = pamar.hallName
			this.showTime = pamar.showTime
			this.infos = uni.getStorageSync('confirminfo')
			this.info = this.infos.time + " " + pamar.language
			this.hallName = pamar.hallName
			uni.setNavigationBarTitle({
				title: pamar.cinemaName
			})
			this.cinemaId = pamar.cinemaId
			this.cinemaName = pamar.cinemaName
			this.hallId = pamar.hallId
			this.showIndex = pamar.showIndex
			this.Price = pamar.cprice
			this.filmId = pamar.filmId
			this.duration = pamar.duration
			if (Config.token == "") {
				uni.navigateTo({
					url: '/pages/login/login?path='
				});
			}


		},
		onShow() {
			this.goseat()
		},
		methods: {
			cancel() {
				let that = this;
				uni.request({
					url: Config.ServerIP + "/wxpay/app/pay/payOrder/cancel/" + that.item.tradeNum+'/6A510782BD7A439F994F8F25A99BEE52',
					method: "PUT",
					header: {
						Authorization: 'Bearer ' + Config.token
					},
					success: (resb) => {
						uni.showToast({
							title: '订单已取消',
							icon: 'none'
						})
					}
				})


			},
			goorderinfo() {

				uni.setStorageSync('ticketinfo', this.item)
				uni.navigateTo({
					url: "../orderinfo/orderinfo"
				})
			},
			getOrderlist() {
				let that = this;
				uni.request({
					url: Config.ServerIP + "/wxpay/app/pay/payOrder/list",
					method: "POST",
					data: {
						direction: "desc",
						pageNum: "1",
						pageSize: "10"
					},
					header: {
						Authorization: 'Bearer ' + Config.token
					},
					success: (res) => {

						if (res.data.code == 200) {
							for (var i = 0; i < res.data.data.length; i++) {
								if (res.data.data[i].orderStatus === "W") {
									that.item = res.data.data[i]
									that.fkshow = true
									return
								}
							}
						} else if (res.data.code == 401) {
							uni.redirectTo({
								url: '/pages/login/login?path=/pages/orderlist/orderlist'
							});
						}

					},
					complete() {
						uni.stopPullDownRefresh()
					}
				})
			},
			goseat() {
				this.getOrderlist()
				let that = this;
				that.seatlist = []
				that.isshow=false
				that.$refs.auiLoading.show();
				uni.request({
					url: Config.ServerIP + '/uaa/auth/refresh',
					method: "POST",
					header: {
						Authorization: 'Bearer ' + Config.token
					},
					success: (resd) => {
						if (resd.data.code == 401) {
							let path = '/pages/selectseat/selectseat?Id=' + that.id + '&name=' + that.name +
								'&address=' + that.address + '&coord=' + that.coord + '&filmId=' + that.filmId
							path = encodeURIComponent(path)
							that.$refs.auiLoading.hide();
							uni.redirectTo({
								url: '../login/login?path=' + path
							})
							return
						}
						uni.request({
							url: Config.ServerIP + "/movie/app/movie/filmShowSeat/list/status",
							method: "POST",
							data: {
								cinemaId: that.cinemaId,
								hallId: that.hallId,

								showIndex: that.showIndex
							},
							header: {
								Authorization: 'Bearer ' + Config.token
							},
							success: (res) => {
								if (res.data.code === 200) {
									that.seatlist = [];
									if (res.data.data != null) {
										if (res.data.data.length <= 0) {
											that.$refs.auiLoading.hide();
											uni.showToast({
												title: '暂无座位数据',
												icon: 'none'
											})
											return
										}
										let seatnum = res.data.data.length
										let X = 0
										let Y = 0
										let miniX = 0
										let miniY = 0

										for (var i = 0; i < seatnum; i++) {
											Y = res.data.data[i].rowId
											if (Y == 0) {
												
												Y = res.data.data[i].rowId
											} else {
												if (X == 0) {
													X = res.data.data[i].columnId

												} else {
													if (res.data.data[i].columnId != X && res.data
														.data[i].rowId == Y) {
														let 	tmpX
														if(res.data.data[i + 1]){
															tmpX = Math.abs(res.data.data[i]
																.columnId - res.data.data[i + 1]
																.columnId)
														}
														
														//console.log(tmpX,res.data.data[i].columnId ,res.data.data[i + 1].columnId)
														if (miniX == 0) {
															miniX = tmpX
														} else {
															if (miniX > tmpX & tmpX != 0) {
																miniX = tmpX
															}
														}

													}
													// console.log(res.data.data[i].rowId)
													// console.log(Y)
													// console.log(miniY)
													if (res.data.data[i].rowId != Y && miniY ==
														0) {

														miniY = Math.abs(res.data.data[i].rowId -
															Y)
													} else if (res.data.data[i].rowId == Y &&
														miniY == 0) {
														let tmpY = Math.abs(res.data.data[i]
															.rowId - res.data.data[i - 1].rowId
														)
															
														if ( tmpY != 0) {
															miniY = tmpY

														}

													}
												}

											}

										}
										console.log(miniX,miniY)
										for (var i = 0; i < seatnum; i++) {
											let pls = res.data.data[i].name.split(':')
											let status = 2
											if (res.data.data[i].status == "Y") {
												status = 0
											} else {
												status = 2
											}
											if (pls[0] != "-1" && pls[1] != "-1") {
												let tmpData = {
													"IsLove": parseInt(res.data.data[i]
														.loveFlag),
													"RowNum": pls[0],
													"ColumnNum": pls[1],
													"YCoord": Math.round(res.data.data[i]
														.rowId / (miniY)),
													"XCoord": Math.round(res.data.data[i]
														.columnId / (miniX)),
													"SeatCode": res.data.data[i]
														.seatId,
													"SeatIndex": res.data.data[i]
														.seatIndex,
													"Status": status,
													"seatId": res.data.data[i].seatId,
													"Price": res.data.data[i].uprice + ""

												};
												that.seatlist.push(tmpData)
											}

										}

										this.isshow = true
										if(that.showt == false){
											setTimeout(function() {
												// 这里就是处理的事件
												//console.log(that.infos.time)
												if (that.infos.time.indexOf('今天') < 0) {
													that.$refs.uToast.show({
														title: that.infos.time.split(" ")[0]+"的场次",
														type: 'default',
														position:"center",
														duration:"3000"
														
													})
												}
												that.showt = true
											}, 1000);
										}
										

									}
								} else {
									return uni.showToast({
										title: '获取错误，请重试',
										icon: 'none'
									})
								}
							},
							complete() {
								that.$refs.auiLoading.hide();
							}

						})
					},
					fail() {
						let path = '/pages/cinemainfo/cinemainfo?Id=' + that.id + '&name=' + that.name +
							'&address=' + that.address + '&coord=' + that.coord + '&filmId=' + that.filmId
						path = encodeURIComponent(path)
						that.$refs.auiLoading.hide();
						uni.redirectTo({
							url: '../login/login?path=' + path
						})
						return
					}
				})
			},

			confirm(e) {
				let that = this
				let seatindexs = ''
				let isok = true
				let amount = 0.00
				let seatinfo = ''
				for (var i = 0; i < e.length; i++) {
					amount = (parseFloat(amount) + parseFloat(e[i].Price)).toFixed(
						2)

					if (i + 1 == e.length) {
						seatinfo = seatinfo + e[i].RowNum + ":" + e[i].ColumnNum
						seatindexs = seatindexs + e[i].SeatIndex
					} else {
						seatinfo = seatinfo + e[i].RowNum + ":" + e[i].ColumnNum +
							","
						seatindexs = seatindexs + e[i].SeatIndex + "|"
					}

					if (e[i].isok == false) {

						isok = false

					}

				}
				if (isok) {
					var date = new Date(); //1. js获取当前时间

					var min = date.getMinutes(); //2. 获取当前分钟

					date.setMinutes(min + 5);
					var h = date.getHours() < 10 ? ('0' + date.getHours()) : date
						.getHours()
					var f = date.getMinutes() < 10 ? ('0' + date.getMinutes()) :
						date.getMinutes()
					let endtime = h + ':' + f
					let seatinfos = seatinfo.replace(/\|/g, ',')
					that.$refs.auiLoading.show();
					uni.request({
						url: Config.ServerIP + '/wxpay/wx/pay/v2/miniAppPay',
						method: "POST",
						data: {
							duration:that.duration,
							cinemaId: that.cinemaId,
							cinemaName: that.cinemaName,
							cityId: Config.loaction.cityId,
							cityName: Config.loaction.name,
							count: e.length,
							countPrice: parseFloat(amount),
							price: parseFloat(amount),
							filmId: that.filmId,
							filmImage: that.smallPhoto,
							filmName: that.title,
							hallId: that.hallId,
							hallName: that.hallName,
							mobile: Config.phone,
							openId: Config.openid,
							selectSeat: seatinfos,
							showIndex: that.showIndex,
							showTime: that.showTime,
							seatInfo: seatindexs,
							appKey:'6A510782BD7A439F994F8F25A99BEE52'

						},
						header: {
							Authorization: 'Bearer ' + Config.token
						},
						success: (res) => {
								console.log(res)
							if (res.data.code == 200) {
								uni.setStorageSync('payinfo',res.data.data)
								that.$refs.auiLoading.hide();
								uni.requestSubscribeMessage({
								  tmplIds: ['ulMLPRXZuPauWmbgkJvs8x-V3du0UGvLccS1BKUm9mE','1zkPNp3eUi2KXnJ-NFrYHpfipBAU4PwIoVBRrLhGVHQ','k6GZgHJrVPitH31pm6PJSQQ7SwZSDGGzL63OBbncNhg'],
								  success () { 
									  
								  },
								  complete(){
									  uni.navigateTo({
									  	url: '/pages/confirmpage/confirmpage?smallPhoto=' +
									  		that.smallPhoto +
									  		'&filmName=' + that.title +
									  		'&info=' + that.info +
									  		'&hallName=' + that.hallName +
									  		'&amount=' + amount +
									  		'&hallId=' + that.hallId +
									  		'&showIndex=' + that.showIndex +
									  		'&seatindexs=' + seatindexs +
									  		'&seatInfo=' + seatinfo +
									  		'&endtime=' + endtime +
									  		'&cinemaId=' + that.cinemaId +
									  		'&cinemaName=' + that.cinemaName +
									  		'&showTime=' + that.showTime +
									  		'&filmId=' + that.filmId
									  });
								  }
								})
								
							} else {
								// that.content = "下单失败，此座可能已被售出"
								// that.show = true
								uni.showToast({
									title: '下单失败，此座可能已被售出',
									icon: 'none'
								})
							}

						}
					})
					
					
				} else {
					uni.showToast({
						title: '旁边座位不要留空',
						icon: 'none'
					})
				}

			}
		}
	}
</script>

<style>

</style>
